package android.storefront.common.utils.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class ActivityUtils {

	private static ActivityUtils activityTransitionUtils = new ActivityUtils();

	public static ActivityUtils getInstance() {
		return activityTransitionUtils;
	}

	public void leftIN_rightOUT_Transition(Activity activity, Context context, Class<?> targetClass) {

		activity.startActivity(new Intent(context, targetClass));

		// if(activity.getWindowManager().getDefaultDisplay().getRotation()==0){
		activity.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
		// }else{
		// activity.overridePendingTransition(android.R.anim.fade_out,
		// android.R.anim.fade_in);
		// }
	}

	public void leftIN_rightOUT_close_Transition(Activity activity, Context context, Class<?> targetClass) {

		activity.startActivity(new Intent(context, targetClass));

		// if(activity.getWindowManager().getDefaultDisplay().getRotation()==0){
		activity.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
		// }else{
		// activity.overridePendingTransition(android.R.anim.fade_out,
		// android.R.anim.fade_in);
		// }
		activity.finish();
	}

	// 自由屏
	public void setScreenOrientation(Activity activity) {
		activity.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
	}

	// //豎屏
	public void setPortpaitScreenOrientation(Activity activity) {
		activity.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 豎屏
	}

	// 全屏
	public void setFullScreenOrientation(Activity activity) {
		Window window = activity.getWindow();
		window.requestFeature(Window.FEATURE_NO_TITLE);
		window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 豎屏
		// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		// //橫屏
	}

	// 初始化版本，从mainfest里面读
	public void initVersion(Context context, TextView welcome_version) {
		try {
			PackageInfo packageInfo = context.getPackageManager() .getPackageInfo("android.storefront.activity", PackageManager.GET_CONFIGURATIONS);
			welcome_version.setText(packageInfo.versionName);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private DisplayMetrics metrics = null;

	private void getMetrics(Activity ac) {
		metrics = new DisplayMetrics();
		ac.getWindowManager().getDefaultDisplay().getMetrics(metrics);
	}

	// 获取屏幕宽高
	public int[] getWidthHight(Activity ac) {
		getMetrics(ac);
		return new int[] { metrics.widthPixels, metrics.heightPixels };
	}

}
